<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */
	defined ('IN_AGORA') or die;

	class BoardModel extends Model {
		var $forum_cat;
		/*
		var $_level;

		function _loadForumChildren($raw_data, $parent, & $result) {
			if (is_null($parent)) $parent = 0;

			foreach ($raw_data as $forum) {
				if ($forum['parent_forum_id'] == $parent) {

					if ($forum['id']) {
						$this_children = array();
						$this->_level++;
						$this->_loadForumChildren($raw_data, $forum['id'],$this_children);
						$this->_level--;
					}

					$forum['level'] = $this->_level;
					
				//	$cat = $forum['cat_name'];
					$cat_id = $forum['cat_id'];
				//	unset($forum['cat_name']);


					if ($parent == 0) {
						$result[$cat_id]['cat_name'] = $forum['cat_name'];
						$result[$cat_id]['cat_id'] = $cat_id;
						$result[$cat_id]['enable'] = $forum['cat_enable'];
						$result[$cat_id]['disp_position'] = $forum['cat_disp_position'];
						
						if ($forum['id']) {
							$result[$cat_id]['forums'][$forum['id']] = & $forum;
							$result[$cat_id]['forums'] += $this_children;
						} else {
							$result[$cat_id]['forums'] = array();
						}
					} else {
						$result[$forum['id']] = $forum;
						$result += $this_children;
					}

				}
			}
		}
		*/
		
		/*		
		function loadForumList() {

		$this->db->setQuery('SELECT'.
						' f.id,f.enable,'.
						' f.parent_forum_id,'.
						' f.forum_name,f.disp_position, c.cat_name,c.id as cat_id, c.disp_position as cat_disp_position, c.enable as cat_enable,'.
						' f.num_posts, f.num_topics, f.forum_desc, f.last_post, f.last_post_id, '.
						' FROM ##__forums AS f'.
						' RIGHT JOIN ##__categories AS c ON f.cat_id = c.id'.
						' ORDER BY cat_disp_position, f.disp_position'
						);
			$this->db->setQuery('SELECT'.
						' f.*,'.
						' c.cat_name,c.id as cat_id, c.disp_position as cat_disp_position, c.enable as cat_enable'.
						' FROM ##__forums AS f'.
						' RIGHT JOIN ##__categories AS c ON f.cat_id = c.id'.
						' ORDER BY cat_disp_position, f.disp_position'
						);
			//print $this->db->_sql;die;
			$forum = $this->db->loadAssocList();
			// dump($forum);
			$result = array();

			$this->_level = 0;
			$this->_loadForumChildren($forum,0,$result);
			return $result;
		}
		*/
		
	    function _addLastPosterFullName (&$forum, $cat_id) {
			if (!array_key_exists('last_poster_fullname', $this->forum_cat[$cat_id]['forums'][$forum['id']]) || (int)$this->forum_cat[$cat_id]['forums'][$forum['id']]['last_poster_fullname'] == 0) {
				$this->db->bind('lp', $forum['last_poster'], 'string');
				$this->db->setQuery('SELECT name FROM #__users WHERE username = :lp');
				$this->forum_cat[$cat_id]['forums'][$forum['id']]['last_poster_fullname'] = $this->db->loadResult();
			}
	    }
	    
		function _checkLastPost(&$forum, $cat_id) {
			if (!$forum['parent_forum_id']) return;
			$parent = &$this->forum_cat[$cat_id]['forums'][$forum['parent_forum_id']];
			if ((int)$parent['last_post'] < (int)$forum['last_post']) {
				$this->forum_cat[$cat_id]['forums'][$forum['parent_forum_id']]['last_post'] = $forum['last_post'];
				$this->forum_cat[$cat_id]['forums'][$forum['parent_forum_id']]['last_post_id'] = $forum['last_post_id'];
				$this->forum_cat[$cat_id]['forums'][$forum['parent_forum_id']]['last_poster'] = $forum['last_poster'];
			}
			$this->forum_cat[$cat_id]['forums'][$forum['parent_forum_id']]['num_topics'] += $forum['num_topics'];
			$this->forum_cat[$cat_id]['forums'][$forum['parent_forum_id']]['num_posts'] += $forum['num_posts'];
			$this->_addLastPosterFullName($parent, $cat_id);
			$this->_checkLastPost($parent, $cat_id);
		}
		

		function loadForumList($hide_disabled = true, $action = 'read') {
		
			$user_model = & Model::getInstance('UserModel');
			$agora_user = $user_model->loadCurrent();
			$access_model = & Model::getInstance('AccessModel');
			$allowed_forums = $access_model->getAllowedForums($agora_user['id'], $action);
			$allowed_forums = implode(',', $allowed_forums);
			$allowed_forums = $allowed_forums ? $allowed_forums : 0;
			
			if (!(int)$agora_user['is_superadmin']) {
				$where = 'WHERE f.id IN (' . $allowed_forums . ') ' . ($hide_disabled ? ' AND f.enable = 1 ' : '');
			} else {
				$where = ($hide_disabled ? ' WHERE f.enable = 1 ' : '');
			}
			$where_cat = ($hide_disabled ? ' WHERE enable = 1 ' : '');
			
			$this->db->setQuery('SELECT cat.*, ist.ref_name FROM ##__categories AS cat LEFT JOIN ##__icon_styles AS ist on cat.icon_style_id=ist.id '.$where_cat.' ORDER BY disp_position');
			$this->forum_cat = $this->db->loadAssocList('id');

			$this->db->setQuery('SELECT f.*, ist.ref_name, p.message as postcontent ' 
							. ' FROM ##__forums AS f ' 
							. ' LEFT JOIN ##__icon_styles AS ist ON f.icon_style_id = ist.id '
							. ' LEFT JOIN ##__posts AS p ON f.last_post_id = p.id AND f.last_post_id <> 0 '
							. $where
							. ' ORDER BY disp_position ');
			$forums = $this->db->loadAssocList();

			foreach ($this->forum_cat as $cat_id => $cat) {
				$this->forum_cat[$cat_id]['cat_id'] = $cat_id;
				$this->forum_cat[$cat_id]['forums'] = array();
			}
			
			foreach ($forums as $forum) {
				$cat_id = $forum['cat_id'];
				if (!isset($this->forum_cat[$cat_id])) continue;
				if (!isset($this->forum_cat[$cat_id]['forums'])) {
				}
				$this->forum_cat[$cat_id]['forums'][$forum['id']] = $forum;
			}

			foreach ($this->forum_cat as $cat_id => $category) {
				$this->forum_cat[$cat_id]['forums'] = parseTree($category['forums'], 0, 0, 'id', 'parent_forum_id', true);

				$forums = &$this->forum_cat[$cat_id]['forums'];
				foreach ($forums as $forum) {
					$lowest = true;
					foreach ($forums as $search_lowest) {
						if ($search_lowest['parent_forum_id'] == $forum['id']) {
							$lowest = false;
							break;
						}
					}

					if ($lowest) {
						$this->_checkLastPost($forum, $cat_id);
					}
					$this->_addLastPosterFullName($forum, $cat_id);
				}
			}
			
			return $this->forum_cat;
		}
	}
?>